package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.MultipleChoice;
import com.han.bom.Professor;
import com.han.bom.Question;
import com.han.bom.TrueOrFalse;
import com.han.dao.AnswerDAO;
import com.han.dao.QuestionDAO;

/**
 * Servlet implementation class ProfessorAddQuestionServlet
 */
@WebServlet("/Professor/ProfessorAddQuestionServlet")
public class ProfessorAddQuestionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static Logger logger = Logger.getLogger(ProfessorAddQuestionServlet.class.getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfessorAddQuestionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		logger.info("Entered ProfessorAddQuestionServlet POST");
		
		Professor professor = null;
		String questionType=null,questionTxt=null,questionCorrect=null,questionCategory=null;
		HttpSession session = request.getSession();
		QuestionDAO questionDAO = new QuestionDAO();
		AnswerDAO answerdao = new AnswerDAO();
		
		if(session.getAttribute("professor")!=null){
			professor = (Professor) session.getAttribute("professor");
		}
		else{
			
		}
		
		if(request.getParameter("questionType")!=null){
			questionType = request.getParameter("questionType");
		}
		else{
			
		}
		
		if(request.getParameter("questionTxt")!=null){
			questionTxt = request.getParameter("questionTxt");
		}
		else{
			
		}
		
		if(request.getParameter("questionCorrect")!=null){
			questionCorrect = request.getParameter("questionCorrect");
		}
		else{
			
		}
		
		if(request.getParameter("questionCategory")!=null){
			questionCategory = request.getParameter("questionCategory");
		}
		else{
			
		}
		
		Question question = null;
		
		if(questionType.equals("MC")){
			
			String questionChoices = null;
			
			if(request.getParameter("questionChoices")!=null){
				questionChoices = request.getParameter("questionChoices");
			}
			else{
				
			}
			
			ArrayList<String> questionChoiceList = new ArrayList<String>();
			
			for(String choice:questionChoices.split(",")){
				questionChoiceList.add(choice);
			}
			
			response.getWriter().println(questionChoiceList+"<br>"+questionChoices.split(","));
			
			question =  new MultipleChoice(questionTxt, questionCorrect, questionChoiceList);
		}
		else if(questionType.equals("TF")){
			
			question =  new TrueOrFalse(questionTxt, questionCorrect);
		}
		else{
			
		}
		
		question.setCategory(questionCategory);
		try {
			
			String suffix = (question instanceof MultipleChoice)?"MC":"TF"; 
			
			question.setQuestionID("Q"+(questionDAO.retrieveMaxQuestionID())+suffix);
		} catch (ClassNotFoundException e1) {
			logger.error(e1.getMessage(), e1);
			e1.printStackTrace();
		} catch (SQLException e1) {
			logger.error(e1.getMessage(), e1);
			e1.printStackTrace();
		}
		
		
		try {
			questionDAO.insertQuestion(question);
			answerdao.addAnswerToQuestion(question);
		} catch (ClassNotFoundException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		
		response.sendRedirect("ProfessorViewQuestionServlet");
	}

}
